  
#ifndef __BMANX_BOMBERLEAGUE_INIT__HEADER_FILE__
#define __BMANX_BOMBERLEAGUE_INIT__HEADER_FILE__


#include "globals.h"
#include "../rsp/c_net.h"
#include "c_world.h"

////////////////////////////////////////////////////////////////////////////////
class bman_net : public rsp_c_net
{
    bool cmd_maninfo( uint32 colpri, uint32 colsec, token nick )
    {
        if( int(_ctrl.get_current_stateid() - _maninfo_frame) < 16 )
            return false;

        binstream* bin = _ctrl.push_cmd();
        if(!bin)  return false;

        man::send_maninfo( *bin, colpri, colsec, nick );
        _maninfo_frame = _ctrl.get_current_stateid();

        return true;
    }


public:

    static rsp_base* world_creator_fn( rsp_base* ref );

    virtual ushort get_version() const
    {
        return World::get_version();
    }

    virtual void draw_world()
    {
        /*
        if(!_maninfo_sent)
        {
            _maninfo_sent = cmd_maninfo( CW._profile._colpri, CW._profile._colsec, CW._profile._nick );
        }
        */

        rsp_c_object_state* os = _ctrl.get_owned_object();
        man* me = os ? (man*)os->bound_object_ptr() : 0;

        if( me
            &&
            (   me->_colorpri != CW._profile._colpri
            ||  me->_colorsec != CW._profile._colsec
            ||  me->_nick != CW._profile._nick
            )
          )
            cmd_maninfo( CW._profile._colpri, CW._profile._colsec, CW._profile._nick );
        
        CW.draw(me);
    }

    void draw_screen( int key )
    {
        if( key == SDLK_ESCAPE  &&  CW.gs.state >= 0 )
        {
            exit_game();
            return;
        }

        if( key &&
            (CW.gs.state == World::GS_ROUND || CW.gs.state == World::GS_ROUND_PREPARE) )
            return;

        if( CW.gs.state == C_World::GSC_INITIAL )             draw_initial(key);
        else if( CW.gs.state == C_World::GSC_OPTIONS )        draw_options(key);
        else if( CW.gs.state == C_World::GSC_CFGCOLOR )       draw_cfgcolor(key);
        else if( CW.gs.state >= 0
            && CW.gs.state != World::GS_ROUND
            && CW.gs.state != World::GS_ROUND_PREPARE )       draw_connect(key);
    }

    virtual void pre_resync( bool initial )
    {
        if(initial)
            _maninfo_frame = _ctrl.get_current_stateid() - 1;

        //resync men if required
        if( initial )
        {
            //rsp_c_object_state* os = rsp_c_object_state::get_from_ptr(&CW);
            //((World*)os->ref_object_ptr())->resync();
        }
    }

    virtual void resync()
    {
    }

    virtual void on_disconnect()
    {
        CW.gs.state = C_World::GSC_INITIAL;
    }


    void exit_game()
    {
        disconnect();
    }

    void set_half_rate( bool hr )
    {
        _bdrawratehalf = hr;
    }

    bman_net()
    {
        //_man = 0;
        //WRef = 0;

        //rsp_c_object<C_World>::register_class( "World", false, _get_world, this );
        //rsp_c_object<bomb>::register_class();
        //rsp_c_object<man>::register_class();
    }


    ///controlled player
    //rsp_c_object_state* _rsp_man;
    //man* _man;

    C_World CW;
    //World* WRef;

    //const char* addr;

    void draw_initial( int key );
    void draw_connect( int key );
    void draw_connect_title();
    void draw_options( int key );
    void draw_cfgcolor( int key );

    uint _maninfo_frame;
};

extern bman_net GW;


#endif //__BMANX_BOMBERLEAGUE_INIT__HEADER_FILE__
